package hpasControllers;

import hpasDAO.HardwareDAO;
import hpasDAO.HardwareItemDAO;
import hpasDAO.RetailerDAO;
import hpasDataEntities.Hardware;
import hpasDataEntities.HardwareItem;
import hpasDataEntities.Retailer;
import hpasUserInterfaces.itemQueryPage.ItemDetailWindow;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Vector;

public class HardwareQueryController implements ActionListener
{
    // 界面元件
    private JRadioButton rbtGeneralType;
    private JComboBox<String> cbDetailedType;
    private ItemDetailWindow detailWindow;
    private JTextField tfKeyword;
    private JButton btSearch;
    private JButton btView;
    private JTable tbResult;
    private JLabel lbResultCaption;
    private int resultSize;

    // 后端接口
    private HardwareDAO hardwareDAO;
    private HardwareItemDAO hardwareItemDAO;
    private RetailerDAO retailerDAO;

    // 方法
    /** 处理监听 **/
    public void actionPerformed(ActionEvent e)
    {
        // 用户按下搜索按钮
        if (e.getSource()==btSearch)
        {
            System.err.println("Search clicked!");
            try
            {
                onSearchHardware();
            }
            catch (Exception exc)
            {
                exc.printStackTrace();
            }
        }
        // 用户按下查看按钮
        else if (e.getSource()==btView)
        {
            try
            {
                openDetailedPage();
            }
            catch (Exception exc)
            {
                exc.printStackTrace();
            }
        }
    }
    /** 搜索硬件 **/
    public void onSearchHardware() throws SQLException
    {
        resetList();
        resultSize=0;
        double lowestPrice=1e20,sumPrice=0.0;
        Vector<Hardware> correspond;
        Vector<HardwareItem> totalItems = new Vector<HardwareItem>();
        correspond = hardwareDAO.getHardware(tfKeyword.getText());
        for (int i=0;i<correspond.size();i++)
        {
            Vector<HardwareItem> items = hardwareDAO.getHardwareItems(correspond.get(i));
            for (int j=0;j<items.size();j++)
            {
                System.err.println(correspond.get(i).getProductName());
                totalItems.add(items.get(j));
                if (resultSize==tbResult.getRowCount())
                {
                    DefaultTableModel model = (DefaultTableModel)tbResult.getModel();
                    model.addRow(new Object[] {null, null, null, null, null, null});
                }
                tbResult.setValueAt(correspond.get(i).getProductName(),resultSize,0);
                tbResult.setValueAt(correspond.get(i).getModelNo(),resultSize,1);
                tbResult.setValueAt(retailerDAO.getRetailer(items.get(j).getRetailerId()).getName(),resultSize,2);
                tbResult.setValueAt(items.get(j).getPrice(),resultSize,3);
                tbResult.setValueAt(retailerDAO.getRetailer(items.get(j).getRetailerId()).getAddress(),resultSize,4);
                tbResult.setValueAt(retailerDAO.getRetailer(items.get(j).getRetailerId()).getHowToRetail(),resultSize,5);
                resultSize++;

                // 价格判断
                if (items.get(j).getRealPrice()<lowestPrice)
                    lowestPrice=items.get(j).getRealPrice();
                sumPrice+=items.get(j).getRealPrice();
            }
        }
        if (resultSize!=0)
            lbResultCaption.setText("已有"+Integer.toString(resultSize)+"个商家进行报价，最低"+
                    ((int)(lowestPrice*100)*0.01)+"元, 平均"+((int)(sumPrice/resultSize*100)*0.01)+"元");
        else
            lbResultCaption.setText("未找到符合条件的硬件");
    }
    /** 清除原有表格 **/
    public void resetList()
    {
        for (int i=0;i<resultSize;i++)
        {
            for (int j=0;j<6;j++)
            {
                tbResult.setValueAt("",i,j);
            }
        }
    }
    /** 弹出详细页 **/
    public void openDetailedPage() throws SQLException
    {
        if (tbResult.getRowCount()==0 || tbResult.getSelectedRow()==-1)
            return;
        int index = tbResult.getSelectedRow();
        Hardware hardware = new Hardware();
        HardwareItem item = new HardwareItem();
        Retailer retailer = new Retailer();
        hardware.setModelNo((String)tbResult.getValueAt(index,1));
        hardware.setProductName((String)tbResult.getValueAt(index,0));
        hardware.setDescription(hardwareDAO.getHardware(hardware.getProductName()).get(0).getDescription());
        retailer.setName((String)tbResult.getValueAt(index,2));
        retailer.setHowToRetail((String)tbResult.getValueAt(index,5));
        retailer.setAddress((String)tbResult.getValueAt(index,4));
        item.setPrice((BigDecimal)tbResult.getValueAt(index,3));
        detailWindow = new ItemDetailWindow(hardware,item,retailer);
        detailWindow.setVisible(true);
    }
    
    // G&S
    public void setKeyword(JTextField tfKeyword)
    {
        this.tfKeyword = tfKeyword;
    }

    public void setSearch(JButton btSearch)
    {
        this.btSearch = btSearch;
    }

    public void setResultBox(JTable tbResult)
    {
        this.tbResult = tbResult;
    }

    public void setResultCaption(JLabel lbResultCaption)
    {
        this.lbResultCaption = lbResultCaption;
    }

    public void setHardwareDAO(HardwareDAO hardwareDAO)
    {
        this.hardwareDAO = hardwareDAO;
        if (this.hardwareItemDAO!=null && this.hardwareDAO!=null)
            connectTwoDAO();
    }

    public void setHardwareItemDAO(HardwareItemDAO hardwareItemDAO)
    {
        this.hardwareItemDAO = hardwareItemDAO;
        if (this.hardwareItemDAO!=null && this.hardwareDAO!=null)
            connectTwoDAO();
    }

    public void connectTwoDAO()
    {
        this.hardwareDAO.setHardwareItemDAO(hardwareItemDAO);
        this.hardwareItemDAO.setHardwareDAO(hardwareDAO);
    }

    public void setRetailerDAO(RetailerDAO retailerDAO)
    {
        this.retailerDAO = retailerDAO;
    }

    public void setDetailWindow(ItemDetailWindow detailWindow)
    {
        this.detailWindow = detailWindow;
    }

    public void setView(JButton btView)
    {
        this.btView = btView;
    }
}
